package pl.jtb.ctl.tabory.ui.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

public class CommonDateUtils {

    private final static Logger log = Logger.getLogger(CommonDateUtils.class);

    public static Date getDate(int year, int month, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    public static int getYear(Date date) {
        if (date == null) {
            return 0;
        }
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.get(Calendar.YEAR);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return 0;
        }
    }

    public static int getMonth(Date date) {
        if (date == null) {
            return 0;
        }
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.get(Calendar.MONTH);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return 0;
        }
    }

    public static int getDay(Date date) {
        if (date == null) {
            return 0;
        }
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return 0;
        }
    }
}
